package com.youkeyi.ddy.cloud.basic.crm.supplier.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.crm.supplier.entity.TblSupplier;
import com.youkeyi.ddy.cloud.basic.crm.supplier.service.TblSupplierService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.supplier.dto.CrmSupplierListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.supplier.dto.CrmSupplierListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.supplier.param.CrmSupplierCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.supplier.param.CrmSupplierDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.supplier.param.CrmSupplierQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.supplier.param.CrmSupplierUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.supplier.CrmSupplierCloudService;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicCrmConstant;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 合作商/供应商表 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-05-11
 */
@Service
public class TblSupplierController implements CrmSupplierCloudService {

    @Autowired
    private TblSupplierService tblSupplierService;

    @Override
    public CrmSupplierListCloudDto query(CrmSupplierQueryCloudParam param) {
        CrmSupplierListCloudDto dto = new CrmSupplierListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getSupplierPrimaryKeys())) {
            param.getSupplierPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCorpKeys())) {
            param.getCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getSourceOrganizationKeys())) {
            param.getSourceOrganizationKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getSourceUserKeys())) {
            param.getSourceUserKeys().removeAll(Collections.singleton(null));
        }

        QueryWrapper<TblSupplier> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getSupplierPrimaryKeys()), TblSupplier::getPrimaryKey, param.getSupplierPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getCorpKeys()), TblSupplier::getCorpKey, param.getCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getSourceOrganizationKeys()), TblSupplier::getSourceOrganizationKey, param.getSourceOrganizationKeys())
                .in(CollectionUtils.isNotEmpty(param.getSourceUserKeys()), TblSupplier::getSourceUserKey, param.getSourceUserKeys())
                .like(StringUtils.isNotBlank(param.getCompanyNameFuzzy()), TblSupplier::getCompanyName, param.getCompanyNameFuzzy())
                .ne(TblSupplier::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;

        Page<TblSupplier> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<TblSupplier> supplierList = tblSupplierService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(supplierList.getRecords())) {
            dto.setTotal(supplierList.getTotal());

            List<CrmSupplierListItemCloudDto> items = new ArrayList<>();
            supplierList.getRecords().forEach(supplier -> {
                CrmSupplierListItemCloudDto item = new CrmSupplierListItemCloudDto();
                item.setSupplierPrimaryKey(supplier.getPrimaryKey());
                item.setCorpKey(supplier.getCorpKey());
                item.setSourceOrganizationKey(supplier.getSourceOrganizationKey());
                item.setSourceOrganizationName(supplier.getSourceOrganizationName());
                item.setSourceUserKey(supplier.getSourceUserKey());
                item.setSourceUserName(supplier.getSourceUserName());
                item.setCategory(supplier.getCategory());
                item.setCategoryName(YoukeyiDdyCloudBasicCrmConstant.CRM_SUPPLIER_CATEGORY.getName(supplier.getCategory()));
                item.setCompanyName(supplier.getCompanyName());
                item.setName(supplier.getName());
                item.setPhone(supplier.getPhone());
                item.setWechatId(supplier.getWechatId());
                item.setPlate(supplier.getPlate());
                item.setPlateKey(supplier.getPlateKey());
                item.setCollaborativeProjectsKey(supplier.getCollaborativeProjectsKey());
                item.setCollaborativeProjects(supplier.getCollaborativeProjects());
                item.setCooperationModeDesc(supplier.getCooperationModeDesc());
                item.setCommissionSharingDesc(supplier.getCommissionSharingDesc());
                item.setRemark(supplier.getRemark());
                item.setAnnexKey(supplier.getAnnexKey());
                item.setIsDeleted(supplier.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(supplier.getCreateTime() != null ? supplier.getCreateTime().getTime() : null);
                item.setCreateUserKey(supplier.getCreateUserKey());
                item.setCreateUserName(supplier.getCreateUserName());
                item.setUpdateTime(supplier.getUpdateTime() != null ? supplier.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(supplier.getUpdateUserKey());
                item.setUpdateUserName(supplier.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(CrmSupplierCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param
                || StringUtils.isBlank(param.getCorpKey()) || StringUtils.isBlank(param.getName())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        TblSupplier supplier = new TblSupplier();
        supplier.setPrimaryKey(SimpleUUidUtils.getUuid());
        supplier.setCorpKey(param.getCorpKey());
        supplier.setSourceOrganizationKey(param.getSourceOrganizationKey());
        supplier.setSourceOrganizationName(param.getSourceOrganizationName());
        supplier.setSourceUserKey(param.getSourceUserKey());
        supplier.setSourceUserName(param.getSourceUserName());
        supplier.setCategory(param.getCategory());
        supplier.setCompanyName(param.getCompanyName());
        supplier.setName(param.getName());
        supplier.setPhone(param.getPhone());
        supplier.setWechatId(param.getWechatId());
        supplier.setPlate(param.getPlate());
        supplier.setPlateKey(param.getPlateKey());
        supplier.setCollaborativeProjectsKey(param.getCollaborativeProjectsKey());
        supplier.setCollaborativeProjects(param.getCollaborativeProjects());
        supplier.setCooperationModeDesc(param.getCooperationModeDesc());
        supplier.setCommissionSharingDesc(param.getCommissionSharingDesc());
        supplier.setRemark(param.getRemark());
        supplier.setAnnexKey(param.getAnnexKey());
        supplier.setRemark(param.getRemark());
        supplier.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        supplier.setCreateUserKey(param.getCreateUserKey());
        supplier.setCreateUserName(param.getCreateUserName());
        supplier.setCreateTime(new Date());
        boolean save = tblSupplierService.save(supplier);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(supplier.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(CrmSupplierUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getSupplierPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        UpdateWrapper<TblSupplier> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(TblSupplier::getPrimaryKey, param.getSupplierPrimaryKey());
        TblSupplier supplier = new TblSupplier();
        supplier.setPrimaryKey(param.getSupplierPrimaryKey());
        supplier.setSourceOrganizationKey(param.getSourceOrganizationKey());
        supplier.setSourceOrganizationName(param.getSourceOrganizationName());
        supplier.setSourceUserKey(param.getSourceUserKey());
        supplier.setSourceUserName(param.getSourceUserName());
        supplier.setCategory(param.getCategory());
        supplier.setCompanyName(param.getCompanyName());
        supplier.setName(param.getName());
        supplier.setPhone(param.getPhone());
        supplier.setWechatId(param.getWechatId());
        supplier.setPlate(param.getPlate());
        supplier.setPlateKey(param.getPlateKey());
        supplier.setCollaborativeProjectsKey(param.getCollaborativeProjectsKey());
        supplier.setCollaborativeProjects(param.getCollaborativeProjects());
        supplier.setCooperationModeDesc(param.getCooperationModeDesc());
        supplier.setCommissionSharingDesc(param.getCommissionSharingDesc());
        supplier.setRemark(param.getRemark());
        supplier.setAnnexKey(param.getAnnexKey());
        supplier.setRemark(param.getRemark());
        supplier.setUpdateUserKey(param.getUpdateUserKey());
        supplier.setUpdateUserName(param.getUpdateUserName());
        supplier.setUpdateTime(new Date());
        boolean save = tblSupplierService.update(supplier, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        return rc;
    }

    @Override
    public ResponseContent delete(CrmSupplierDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getSupplierPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<TblSupplier> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(TblSupplier::getPrimaryKey, param.getSupplierPrimaryKeys());

        TblSupplier supplier = new TblSupplier();
        supplier.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        supplier.setUpdateUserKey(param.getUpdateUserKey());
        supplier.setUpdateUserName(param.getUpdateUserName());
        supplier.setUpdateTime(new Date());
        boolean save = tblSupplierService.update(supplier, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }
        return rc;
    }
    
}
